﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using TeacherLeftHand.DataAccess;

using TeacherLeftHand.Model;

using TeacherLeftHand.Util;

namespace TeacherLeftHand.ViewModel
{
    public class TeachingPlanViewModel : ViewModelBase, IDataErrorInfo
    {
        #region Constructor
        public TeachingPlanViewModel()
        {
            _teachingPlan = new TeachingPlan(classLevel: 1);
            _teachingPlanAccessor = new TeachingPlanAccessor();
            _knowledgeItemAccessor = new KnowledgeItemAccessor();

          //_selectionSubjectList = new SelectionList<string>(_subjectList);
            //Save Command

            //ExportCommand
        }
        #endregion

        #region Properties
        private TeachingPlanAccessor _teachingPlanAccessor;
        private readonly KnowledgeItemAccessor _knowledgeItemAccessor;
        private readonly TeachingPlan _teachingPlan;
        private FileExporter _fileExporter;
        //private ObservableCollection<string> _subjectList;

        public ObservableCollection<int> ClassLevelList
        {
            get
            {
                var list = new ObservableCollection<int>();
                for (int i = 1; i <= 5; i++)
                {
                    list.Add(i);
                }
                return list;
            }
        }

        public int ClassLevel
        {
            get { return _teachingPlan.ClassLevel; }
            set
            {
                if (_teachingPlan.ClassLevel != value)
                {
                    _teachingPlan.ClassLevel = value;
                    RaisePropertyChanged("ClassLevel");
                    //Messenger.Default.Send(value, "SelectedClassLevel");
                }
            }
        }

        public DateTime? ComposeDate
        {
            get { return _teachingPlan.ComposeDate; }
            set
            {
                if (_teachingPlan.ComposeDate != value)
                {
                    _teachingPlan.ComposeDate = value;
                    RaisePropertyChanged("ComposeDate");
                }
            }
        }

        public string Note
        {
            get { return (string)_teachingPlan.Note; }
            set
            {
                if (_teachingPlan.Note != value)
                {
                    _teachingPlan.Note = value;
                    RaisePropertyChanged("Note");
                }
            }
        }

        public string Objective
        {
            get { return (string)_teachingPlan.Objective; }
            set
            {
                if (_teachingPlan.Objective != value)
                {
                    _teachingPlan.Objective = value;
                    RaisePropertyChanged("Objective");
                }
            }
        }

        public string Preparation
        {
            get { return (string)_teachingPlan.Preparation; }
            set
            {
                if (_teachingPlan.Preparation != value)
                {
                    _teachingPlan.Preparation = value;
                    RaisePropertyChanged("Preparation");
                }
            }
        }

        public int Session
        {
            get { return _teachingPlan.Session; }
            set
            {
                if (_teachingPlan.Session != value)
                {
                    _teachingPlan.Session = value;
                    RaisePropertyChanged("Session");
                }
            }
        }

        public string Subject
        {
            get { return _teachingPlan.Subject; }
            set
            {
                if (_teachingPlan.Subject != value)
                {
                    _teachingPlan.Subject = value;
                    RaisePropertyChanged("Subject");
                    //Messenger.Default.Send(Subject,"SelectedSubject");
                }
            }
        }

        public DateTime? TeachingDate
        {
            get
            {
                return _teachingPlan.TeachingDate;
            }
            set
            {
                if (_teachingPlan.TeachingDate != value)
                {
                    _teachingPlan.TeachingDate = value;
                    RaisePropertyChanged("TeachingDate");
                }
            }
        }

        public string Topic
        {
            get { return _teachingPlan.Topic; }
            set
            {
                if (_teachingPlan.Topic != value)
                {
                    _teachingPlan.Topic = value;
                    RaisePropertyChanged("Topic");
                }
            }
        }

        public int TopicNo
        {
            get { return _teachingPlan.TopicNo; }
            set
            {
                if (_teachingPlan.TopicNo != value)
                {
                    _teachingPlan.TopicNo = value;
                    RaisePropertyChanged("TopicNo");
                }
            }
        }

        public ObservableCollection<string> SubjectList
        {
            get
            {
                return _knowledgeItemAccessor.GetSubjectList();
            }
        }

        /// <summary>
        /// Gets the TopicList property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<string> TopicList
        {
            get
            {
                return _knowledgeItemAccessor.GetTopicList();
            }
        }
        #region Commands
        public ICommand SaveCommand
        {
            get;
            private set;
        }

        public ICommand ExportCommand
        {
            get;
            private set;
        }

        public ObservableCollection<ActivityViewModel> Activities
        {
            get;
            private set;
        }

        public ICommand OpenCommand
        {
            get;
            private set;
        }

        public ICommand RemoveActivityCommand
        {
            get;
            private set;
        }

        public ICommand AddNewActivityCommand
        {
            get;
            private set;
        }

        public ICommand LoadCommand
        {
            get;
            private set;
        }
        #endregion

        #endregion

        #region Implementation of IDataErrorInfo

        /// <summary>
        /// Gets the error message for the property with the given name.
        /// </summary>
        /// <returns>
        /// The error message for the property. The default is an empty string ("").
        /// </returns>
        /// <param name="columnName">The name of the property whose error message to get. </param>
        public string this[string columnName]
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <returns>
        /// An error message indicating what is wrong with this object. The default is an empty string ("").
        /// </returns>
        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region methods
        public bool AddNewActivity()
        {
            throw new System.NotImplementedException();
        }

        public bool Export()
        {
            throw new System.NotImplementedException();
        }

        public void RemoveActivity()
        {
            throw new System.NotImplementedException();
        }

        public bool Save()
        {
            throw new NotImplementedException();
        }

        public bool Load()
        {
            throw new System.NotImplementedException();
        }
        #endregion
    }
}
